/*
 * Copyright 2008 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package sample {
	import org.splink.library.logging.ILogger;
	import org.splink.library.logging.ILoggerFactory;
	import org.splink.library.logging.LogLevel;
	import org.splink.library.logging.LoggerFactory;
	import org.splink.library.logging.LoggerProvider;
	import org.splink.library.logging.logoutput.QLogOutput;
	import org.splink.library.logging.logoutput.TraceOutput;
	import org.splink.library.queue.QEvent;
	import org.splink.resource.ExternalResourceProcessor;
	import org.splink.resource.IResourceProcessor;
	import org.splink.resource.ResourceProcessorEvent;
	import org.splink.resource.ResourceProgress;
	import org.splink.resource.ResourceProvider;
	import org.splink.resource.resourcebundles.resourcebundle.fonts.FontData;

	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * A sample
	 * 
	 * @author Max Kugland
	 */
	public class SampleApplication extends Sprite {
		private static var _logger : ILogger;
		private var _textField : TextField;

		/**
		 * Configures the splinklibrary logger 
		 */
		private function configureLogger() : void {
			var id : String = "max";
			var factory : ILoggerFactory = new LoggerFactory(id);
			factory.addLogOutput(new QLogOutput());			factory.addLogOutput(new TraceOutput());
			LoggerProvider.addLoggerFactory(factory);
			
			_logger = LoggerProvider.getLogger(id, SampleApplication);
		}

		public function SampleApplication() {
			configureLogger();
			_logger.log(LogLevel.INFO, "Starting SampleApplication");
			
			createProgressTextField();

			var processor : IResourceProcessor = new ExternalResourceProcessor("resourcebundles.xml", "at_DE");
			processor.register(QEvent.ERROR, onError);			processor.register(QEvent.COMPLETE, onComplete);
			processor.register(ResourceProcessorEvent.PROGRESS, onProgress);
			processor.start();
		}

		/**
		 * Creates a <code>TextField</code> to display the progress of the <code>IResourceProcessor</code>
		 * Because there is no embedded font available at the time of loading the resources, the Arial 
		 * system font is used.  
		 */
		private function createProgressTextField() : void {
			var textFormat : TextFormat = new TextFormat();
			textFormat.size = 12;
			textFormat.font = "Arial";
			
			_textField = new TextField();
			_textField.defaultTextFormat = textFormat;
			_textField.autoSize = TextFieldAutoSize.LEFT;
			_textField.text = "initializing...";
			_textField.x = 0;
			_textField.y = height + 20;
			
			addChild(_textField); 
		}		

		private function onProgress(e : ResourceProcessorEvent) : void {
			var progress : ResourceProgress = e.resourceProgress;
			
			var br : String = "\r\n";
			_textField.text = "";
			_textField.appendText(progress.currentId + " " + progress.current + " / " + progress.total + br);
			_textField.appendText(Math.round(progress.currentLoadedBytes / 1024) + " kb / " + Math.round(progress.currentTotalBytes / 1024) + " kb " + progress.currentPercent + "%" + br);			_textField.appendText(Math.round(progress.totalLoadedBytes / 1024) + " kb / " + Math.round(progress.totalBytes / 1024) + " kb " + progress.totalPercent + "%" + br);
		} 

		private function onError(e : QEvent) : void {
			_logger.log(LogLevel.ERROR, "ERROR " + e.errorMessage);
		}

		private function onComplete(e : QEvent) : void {
			removeChild(_textField);
			
			testAsset();
			testFont();
			testLibrary();
		}

		/**
		 * Samples the usage of dynamic class loading. The <code>TestLibrary</code> class is not 
		 * compiled into the sample.swf but loaded dynamically by  splinkresource into the current 
		 * <code>ApplicationDomain</code> and therefore the  <code>TestLibrary</code> class is 
		 * accessible.
		 */
		private function testLibrary() : void {
			_logger.log(LogLevel.TRACE, new TestLibrary().test());
		}

		private function testAsset() : void {
			addChild(ResourceProvider.instance().getAssetById(Assets.SPLINK_ASSET));
		}

		private function testFont() : void {
			var textFont : FontData = ResourceProvider.instance().getFontDataByType("text");			addChild(createTextfield(textFont)).y = 170; 
						var systemFont : FontData = ResourceProvider.instance().getFontDataByType("system");
			addChild(createTextfield(systemFont)).y = 200; 
		}

		private function createTextfield(font : FontData) : TextField {
			var t : TextField = new TextField();
			t.autoSize = TextFieldAutoSize.LEFT;
			var tf : TextFormat = new TextFormat();
			tf.font = font.id;
			tf.size = 20;
			
			var xml : XML = new XML(ResourceProvider.instance().getFileById("test-text"));
			t.htmlText = xml.text();
			t.embedFonts = !font.isSystemFont;
			t.setTextFormat(tf);
			return t;
		}
	}
}
